﻿using Common.Contract.Authentication.Record;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.CustomerManager;
using Common.Contract.Customer.Response.CustomerManager;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using Customer.Model.Interface;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Customer.Core.ServiceCommand.CustomerManager
{
    public class RetriveCountCustomerAlertServiceCommand : ProviderServiceCommand<IProviderFactory, RetriveCountCustomerAlertRequest, RetrieveCountCustomerAlertResponse>
    {
        #region Properties
        public UserRecord Dao;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        #endregion
        public RetriveCountCustomerAlertServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetriveCountCustomerAlertRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrieveCountCustomerAlertResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<CountAlertCustomerRecord> records = new List<CountAlertCustomerRecord>();
                int dao = 0;
                bool isDao = int.TryParse(Dao.Dao, out dao);
                FilterRecord filterRecord = new FilterRecord();
                switch (Dao.GroupIdentifier)
                {
                    case "1":
                        break;
                    case "2":
                        filterRecord.Region = Dao.Zone;
                        break;
                    case "3":
                        filterRecord.Branch = Dao.BranchId;
                        break;
                    case "4":
                        filterRecord.Dao = Dao.Dao;
                        break;
                    default:
                        break;
                }
                var alertBadbank = Providers.CustomerAlerts.RetrieveCustomerOverdue(out totalRecord, Dao, "vi", filterRecord);
                var alertLoan = Providers.CustomerAlerts.RetrieveCustomerLoanMatdt(out totalRecord, Dao, "vi", filterRecord);
                var alertDeposit = Providers.CustomerAlerts.RetrieveTblAlertDepositMatdt(out totalRecord, Dao, "vi", filterRecord);
                var alertCasa = Providers.CustomerAlerts.RetrieveCustomerAlertCasa(out totalRecord, Dao, "vi", filterRecord);
                var alertBL = Providers.CustomerAlerts.RetrieveCustomerAlertBL(out totalRecord, Dao, "vi", filterRecord);
                var alertLimit = Providers.CustomerAlerts.RetrieveCustomerAlertLimit(out totalRecord, Dao, "vi", filterRecord);
                var alertInactive = Providers.CustomerAlerts.RetrieveCustomerAlertInactive(out totalRecord, Dao, "vi", filterRecord);
                var alertCelebration = Providers.CustomerAlerts.RetrieveCustomerCelebrationByDao(out totalRecord, Dao, filterRecord, "vi");
                string maxMonth = alertCasa.Max(x => x.Month).ToString();
                string minMonth = alertCasa.Min(x => x.Month).ToString();
                DateTime dateNow = DateTime.Today;
                records.Add(new CountAlertCustomerRecord
                {
                    AlertTitle = "Badbank",
                    CountCust = alertBadbank.Count(),
                    CountNewCust = alertBadbank.Where(x => x.NoOvdDays == 1).Count()
                });
                records.Add(new CountAlertCustomerRecord
                {
                    AlertTitle = "Loan",
                    CountCust = alertLoan.Count,
                    CountNewCust = alertLoan.Where(x => x.Matdt.Date == DateTime.Now.Date).Count()
                });
                records.Add(new CountAlertCustomerRecord
                {
                    AlertTitle = "Deposit",
                    CountCust = alertDeposit.Count,
                    CountNewCust = alertDeposit.Where(x => x.Matdt.Date == DateTime.Now.Date).Count()
                });
                records.Add(new CountAlertCustomerRecord
                {
                    AlertTitle = "Casa",
                    CountCust = alertCasa.Where(x => x.Month.Equals(maxMonth)).Count(),
                    CountNewCust = alertCasa.Where(x => x.Month.Equals(maxMonth)).Count() - alertCasa.Where(x => x.Month.Equals(minMonth)).Count()
                });
                records.Add(new CountAlertCustomerRecord
                {
                    AlertTitle = "Celebration",
                    CountCust = alertCelebration.Count,
                    CountNewCust = alertCelebration.Where(x => x.BirthIncorpDate.Value.Day == DateTime.Now.Day && x.BirthIncorpDate.Value.Month == DateTime.Now.Month).Count()
                });
                records.Add(new CountAlertCustomerRecord
                {
                    AlertTitle = "BL",
                    CountCust = alertBL.Count,
                    CountNewCust = alertBL.Where(x => x.MaturityDate.Value.Day == DateTime.Now.Day && x.MaturityDate.Value.Month == DateTime.Now.Month).Count()
                });
                records.Add(new CountAlertCustomerRecord
                {
                    AlertTitle = "Limit",
                    CountCust = alertLimit.Count,
                    CountNewCust = alertLimit.Where(x => x.ExpiryDate == DateTime.Now.Date).Count()
                });
                records.Add(new CountAlertCustomerRecord
                {
                    AlertTitle = "Inactive",
                    CountCust = alertInactive.Count
                });
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;

            }

            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
